#include "../lib/IA.hpp"
#include "../lib/Unite.hpp"
#include "../lib/Coordonnes.hpp"
#include <cassert>
#include <ctime>
#include <cmath>

#define Rapport_ville 30
#define Rapport_unite 15




static Colonisation  COLO[12][12];



static int Num_Unite (Unite * G , Partie*P)
{
    int ret;

    for (unsigned int i = 0 ; i < P->IA->nb_unite ; i ++ )
    {
        if ( G == P->IA->tab_unite[i])
        {
            ret = i;
        }
    }

    return ret;
}

static bool Ennemi ( Unite * U, Partie * P, int x, int y)
{
    bool ret=false;
    Coord* C;
    Coord* CU;
    CU = Retourne_Coord( U);

    for ( unsigned int i = 0 ; i < P->Jhumain->nb_unite ; i ++ )
    {
        C = Retourne_Coord( P->Jhumain->tab_unite[i]);

        if (  ( Retourne_X(C) == (Retourne_X( CU)+x) ) && ( Retourne_Y(C)== ( Retourne_Y(CU)+ y )))
        {
            i = P->Jhumain->nb_unite;
            ret = true;
        }
    }

    for ( unsigned int i = 0 ; i < P->Jhumain->nb_ville ; i ++ )
    {
        C = Retourne_Coord_Ville( P->Jhumain->tab_ville[i]);

        if (  ( Retourne_X(C) == (Retourne_X( CU)+x) ) && ( Retourne_Y(C)== ( Retourne_Y(CU)+ y )))
        {
            i = P->Jhumain->nb_ville;
            ret = true;
        }
    }
    return ret;
}

int Action_Guerrier ( Unite * G , Partie*P)
{
    int ret = 0;
     unsigned int Barx, Bary;
    int i = Num_Unite( G, P);
    srand (time(NULL));

    int aleatoire4 = rand()%4;
    aleatoire4= aleatoire4+2;
    int aleatoirebool = rand()%2;

    Barx = 0;
    for (unsigned int i = 0 ; i < P->Jhumain->nb_ville; i ++)
    {
        Barx = sqrt ( Barx*Barx + P->Jhumain->tab_ville[i]->pos->x * P->Jhumain->tab_ville[i]->pos->x ) ;
    }
    Bary = 0;
    for (unsigned int i = 0 ; i < P->Jhumain->nb_ville; i ++)
    {
        Bary = sqrt ( Bary*Bary + P->Jhumain->tab_ville[i]->pos->y * P->Jhumain->tab_ville[i]->pos->y ) ;
    }


    if ( Ennemi( G, P, -1,0))
        ret = 6;
    else if ( Ennemi( G, P, 0,-1))
        ret = 7;
    else if ( Ennemi( G, P, 0,1))
        ret = 8;
    else if ( Ennemi( G, P, 1,0))
        ret = 9;
    else
    {
        if ( aleatoirebool == 0)
            ret = aleatoire4;
        else
        {
            if ( (i + (int )aleatoire4)%2 == 0  )
            {
                if ( Barx < G->pos->x)
                {
                    ret = 2;
                }
                else if ( Barx > G->pos->x)
                {
                    ret = 5;
                }
                else if ( Bary < G->pos->y)
                {
                    ret = 3;
                }
                else if ( Bary > G->pos->y)
                {
                    ret = 4;
                }
            }
            else
            {
                if ( Bary < G->pos->y)
                {
                    ret = 3;
                }
                else if ( Bary > G->pos->y)
                {
                    ret = 4;
                }
                else if ( Barx < G->pos->x)
                {
                    ret = 2;
                }
                else if ( Barx > G->pos->x)
                {
                    ret = 5;
                }
            }
        }
    }


    return ret;
}


static void Grise_Colonisation_C ( Partie * P, int x, int y)
{
   if ( !Norme_Villes_Inf_Trois( x, y, P )  )
   {
       COLO[x][y]= deja_colonise;
   }
}

static void Grise_Colonisation ( Partie * P)
{

    for (int i = 0 ; i < 12 ; i++)
    {
        for(int j =0 ; j < 12 ; j++)
        {
            Grise_Colonisation_C(P, i, j);
        }
    }
}


static bool Coince ( Coord * C, Partie * P )
{
    bool b = false;
    if ( Collision( C->x+1, C->y , P))
        if (Collision( C->x-1, C->y , P))
            if (Collision( C->x, C->y+1 , P))
                if (Collision( C->x, C->y-1 , P))
                {
                    b = true;
                }
    return b;

}

static int Choisit_Prochaine_Coordonnee( Coord * C, Partie* P)
{
    int retour;
    bool go;
    int alea;

    srand (time(NULL));


    go = false;
    while (go == false)
    {
        alea = rand()%4;
        if ( Coince( C, P))
        {
              retour = 10;
              go = true;
        }

        switch ( alea)
        {
            case 0 :
                if ( !Collision( C->x+1, C->y , P))
                {
                    retour = 5;
                    go = true;
                }
                break;
            case 1 :
                if ( !Collision( C->x-1, C->y , P))
                {
                    retour = 2;
                    go = true;
                }
                break;
            case 2:
                if ( !Collision( C->x, C->y +1, P))
                {
                    retour = 4;
                    go = true;
                }
                break;
            case 3 :
                if ( !Collision( C->x, C->y -1, P))
                {
                    retour = 3;
                    go = true;
                }
                break;
        }
    }


    return retour;
}

void Initialisation_IA( Partie * P)
{
    for (int j = 0 ; j < 12 ; j++)
    {
        for(int i = 0 ; i < 12 ; i++)
        {

            Grise_Colonisation_C( P, i, j );

            switch(P->C->CARTE[i+j*P->C->taille])
            {
                case mer:
                    COLO[i][j]=pas_possible;
                    break;
                default:
                    COLO[i][j]=possible;
                    break;
            }

        }
    }
}

void Mise_A_Jour_IA ( Partie * P )
{
    Grise_Colonisation( P);
}


int Action_Colon ( Unite * C , Partie * P)
{
    int retour;
    int cas = 1;


    if( COLO[C->pos->x][C->pos->y] == possible )
    {
        retour = 1;
    }
    else
    {
        cas  = Choisit_Prochaine_Coordonnee( C->pos, P);
        // cas = 2 : gauche, cas = 3 : haut, cas = 4 : bas , cas = 5 : droite.
        retour = cas;
    }


    return retour;
}


int Choix_IA_Unite(Partie * P, int i)
{
    int ret = 0;
    Unite * U = Retourne_Unite(P->IA,i);
    Mise_A_Jour_IA( P);

    if(P->IA->tab_unite[i]->nom == colon)
    {
        ret = Action_Colon (U,P);
    }
    else if(P->IA->tab_unite[i]->nom == guerrier)
    {
        ret = Action_Guerrier (U,P);
    }

    return ret;

}


int Choix_IA_Ville ( Partie * P, int i)
{
    int ret = 0;

    srand (time(NULL));

    int alea1 = rand()%1;
    int alea2 = rand()%4;


    if(P->IA->nb_unite < 36)
    {
        Coord * C = Retourne_Coord_Ville(P->IA->tab_ville[i]);
        unsigned int x = Retourne_X (C);
        unsigned int y = Retourne_Y (C) - 1;

        if(P->IA->nb_unite == 0)
        {
            ret = 1;
        }
        else
        {
            for(unsigned int i = 0; i < P->IA->nb_unite; i++)
            {
                Unite * U = Retourne_Unite(P->IA,i);
                Coord * C1 = Retourne_Coord(U);
                unsigned int xu = Retourne_X(C1);
                unsigned int yu = Retourne_Y(C1);

                if(x == xu && y == yu)
                {
                    ret = 0;
                }
                else
                {
                    if(P->IA->nb_ville < 8)
                    {
                        if(alea1 == 0)
                        {
                            if(alea2 == 0)
                            {
                                ret = 1;
                            }
                            else
                            {
                                ret = 2;
                            }
                        }
                    }
                    else if(alea2 == 0)
                    {
                        ret = 0;
                    }
                    else
                    {
                       ret = 2;
                    }
                }
            }
        }


    }

    return ret;
}

